
package io.dbw.modules.business.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.dbw.common.annotation.SysLog;
import io.dbw.common.enums.BusinessTypeEnum;
import io.dbw.common.utils.*;
import io.dbw.modules.business.dto.UserDTO;
import io.dbw.modules.business.entity.SysUserEntity;
import io.dbw.modules.business.form.PasswordForm;
import io.dbw.modules.business.service.SysUserRoleService;
import io.dbw.modules.business.service.SysUserService;
import io.dbw.modules.business.vo.CommonUserVO;
import io.dbw.modules.business.vo.IdsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

@Validated
@RestController
@RequestMapping("/sys/user")
@Api(tags = "学生管理")
public class SysUserController extends AbstractController {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysUserRoleService sysUserRoleService;

	/**
	 * 学生列表
	 */
	@PostMapping("/list")
	@ApiOperation(value="列表查询")
	@RequiresPermissions("system:user:list")
	public Result<PageUtils<SysUserEntity>> list(@RequestBody UserDTO userDTO){
		Map<String, Object> params=MapToDTOUtil.dtoTOMap(userDTO);
		//只有超级管理员，才能查看所有管理员列表
		CommonUserVO user = getUser();
		if(user.getManagerFlag().equals(Constant.SYSTEM_COMMON_N)){
			return Result.error("没有管理员权限,不能查询");
		}
		PageUtils page = sysUserService.queryPage(params);
		return writeSuccess(page);
	}


	/**
	 * 获取登录的学生信息
	 */
	@GetMapping("/getUserInfo")
	@ApiOperation(value="获取登录学生信息")
	public Result<CommonUserVO> info(){
		CommonUserVO userVO= getUser();
		SysUserEntity u= BeanUtil.copyProperties(userVO, SysUserEntity.class);
		return writeSuccess(u);
	}
	
	/**
	 * 修改登录学生密码
	 */
	@SysLog(value = "学生",businessType = BusinessTypeEnum.PASSWORD_UPDATE)
	@PostMapping("/password")
	@ApiOperation(value="登录学生修改自己密码")
	public Result password(@RequestBody PasswordForm form){
		AssertUtils.isBlank(form.getNewPassword(), "新密码不为能空");
		sysUserService.updatePassword(getUserId(),   form.getNewPassword());
		return writeSuccess();
	}

	/**
	 * 修改学生密码
	 */
	@SysLog(value = "学生",businessType = BusinessTypeEnum.PASSWORD_UPDATE)
	@PostMapping("/updatePasswordById")
	@ApiOperation(value="列表修改学生密码")
	@RequiresPermissions("system:user:reset")
	public Result updatePasswordById(@RequestBody PasswordForm form){
		AssertUtils.isNull(form.getUserId(), "学生ID不为能空");
		AssertUtils.isBlank(form.getNewPassword(), "新密码不为能空");
		sysUserService.updatePassword(form.getUserId(),  form.getNewPassword());
		return writeSuccess();
	}
	
	/**
	 * 学生信息
	 */
	@GetMapping("/info")
	@RequiresPermissions("system:user:info")
	@ApiOperation(value="根据id获取学生信息")
	@ApiImplicitParam(paramType = "query",name = "userId",value ="学生id",dataType ="long",required = true)
	public Result<SysUserEntity> info(@NotNull(message = "学生id不能为空") Long userId){
		SysUserEntity user = sysUserService.getById(userId);
		//获取学生所属的角色列表
		List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
		user.setRoleIdList(roleIdList);
		return writeSuccess(user);
	}
	
	/**
	 * 保存学生
	 */
	@SysLog(value = "学生",businessType = BusinessTypeEnum.INSERT)
	@PostMapping("/save")
	@RequiresPermissions("system:user:add")
	@ApiOperation(value="保存")
	public Result save(@Validated @RequestBody SysUserEntity user){
		AssertUtils.isBlank(user.getPassword(),"密码不能为空");
		user.setUserId(null);
		Long count=sysUserService.count(new QueryWrapper<SysUserEntity>()
				.eq("user_name",user.getUserName()));
		if (count>0){
			return writeFail("已存在该编号");
		}
		sysUserService.saveUser(user);
		return writeSuccess();
	}
	
	/**
	 * 修改学生
	 */
	@SysLog(value = "学生",businessType = BusinessTypeEnum.UPDATE)
	@PostMapping("/update")
	@RequiresPermissions("system:user:edit")
	@ApiOperation(value="修改")
	public Result update(@Validated @RequestBody SysUserEntity user){
		AssertUtils.isNull(user.getUserId(),"学生ID不能为空");
		if(getUser().getManagerFlag().equals(Constant.SYSTEM_COMMON_Y)){
			SysUserEntity s = sysUserService.getById(user.getUserId());
			if(null==s){
				return Result.error("该记录不存在");
			}
			SysUserEntity u=sysUserService.getOne(new QueryWrapper<SysUserEntity>()
					.eq("user_name",user.getUserName()));

			if (null!=u && !u.getUserId().equals(user.getUserId()) ){
				return writeFail("已存在该学生名,请重新输入");
			}
			user.setRoleEditFlag(true);
			if(  s.getManagerFlag().equals(user.getManagerFlag())){
				user.setRoleEditFlag(false);
			}
			user.setDeleteFlag(Constant.SYSTEM_COMMON_N);
			sysUserService.updateUser(user);
		}else{
			return writeFail("您不是管理员,不能操作");
		}
		return writeSuccess();
	}
	
	/**
	 * 删除学生
	 */
	@SysLog(value = "学生",businessType = BusinessTypeEnum.DELETE)
	@PostMapping("/delete")
	@RequiresPermissions("system:user:delete")
	@ApiOperation(value="删除")
	public Result delete(@RequestBody IdsVO idsVO){
		CommonUserVO user = getUser();
		if(user.getManagerFlag().equals(Constant.SYSTEM_COMMON_N)){
			return Result.error("您不是管理员，无权操作");
		}
		for (Long id:idsVO.getIds()){
			sysUserService.removeById(id);
		}
		return writeSuccess();
	}


	/**
	 * 导出定制数据
	 */
	@GetMapping("/export")
	@ApiOperation(value="导出")
	@RequiresPermissions("system:user:export")
	public Result export(@ApiIgnore @RequestParam Map<String, Object> param){
		ExcelUtil<SysUserEntity> util = new ExcelUtil<>(SysUserEntity.class);
		List<SysUserEntity> list = sysUserService.list();
		return util.exportExcel(list, "学生列表");
	}


}
